package com.lcw.lostandfound.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.lcw.lostandfound.pojo.Comment;
import com.lcw.lostandfound.pojo.Goods;
import com.lcw.lostandfound.pojo.User;
import com.lcw.lostandfound.service.CategoryService;
import com.lcw.lostandfound.service.CommentService;
import com.lcw.lostandfound.service.GoodsService;
import com.lcw.lostandfound.service.ImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author lcw
 */
@Controller
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ImgService imgService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/detail")
    public String detail(@RequestParam(name = "id")Integer id, Model model){
        goodsService.updateViews(id);
        Goods goods = goodsService.findById(id);
        String category = categoryService.convertCategory(goods.getCategory());
        goods.setCategory(category);
        model.addAttribute("msg",goods);
        return "detail";
    }

    //寻物列表
    @RequestMapping("/findMessage")
    public String findMessage(@RequestParam(name = "type")Integer type,Model model,@RequestParam(name = "pageNum",required = false,defaultValue = "1")Integer pageNum,@RequestParam(name = "pageSize",required = false,defaultValue = "30")Integer pageSize){
        PageInfo<Goods> pages = goodsService.listFindMsg(pageNum, pageSize, type);
        if (pages.getPages()==0){
            pages.setPages(1);
        }
        model.addAttribute("info",pages);
        model.addAttribute("type",type);
        return "FindMessage";
    }

    //搜索
    @RequestMapping("/search")
    public String search(Model model,@RequestParam(name = "pageNum",required = false,defaultValue = "1")Integer pageNum,@RequestParam(name = "pageSize",required = false,defaultValue = "10")Integer pageSize,@RequestParam(name = "key",required = false)String key){
        //redis热搜功能实现,添加热搜
        redisTemplate.opsForZSet().incrementScore("sortSearch",key,1);
        PageInfo<Goods> pages = goodsService.findByLike(pageNum, pageSize, key);
        if (pages.getPages()==0){
            pages.setPages(1);
        }
        model.addAttribute("info",pages);
        model.addAttribute("search",key);
        System.out.println(pages.getPages());
        return "SearchMessage";
    }
    //判断是否已找到
    @ResponseBody
    @RequestMapping(value = "/seekOut",method = RequestMethod.POST )
    public String seekOut(@RequestBody String str){
        ObjectMapper obj=new ObjectMapper();
        try{
            Map map=obj.readValue(str,Map.class);
            Integer id=(Integer) map.get("id");
            goodsService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
}
